import java.sql.*;

public class Main {
    public static void main(String[] args) {
//1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.创建连接
            String url="jdbc:mysql://127.0.0.1:3306/library?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url,"root","root");

            if(connection!=null){
                System.out.println("连接成功");
            }
            //3.编写sql语句
            String sql="select*from admin";

            // 4、预编译sql
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            // 5、有参数设置参数

            // 6、执行sql
            ResultSet resultSet = preparedStatement.executeQuery();

            // 7、遍历结果集
            while (resultSet.next()){
                int id = resultSet.getInt(1);
                String username = resultSet.getString(2);
                String password = resultSet.getString(3);
                System.out.println(id+"-----"+username+"------"+password);
            }

            // 8、关闭资源
            resultSet.close();
            preparedStatement.close();
            connection.close();




        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //2.

        //3.

       //4.

    }
}